package de.velvettear.squirrel.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import de.velvettear.squirrel.kernel.File;
import android.util.Log;

public class TimeHelper {

	public TimeHelper() {
		
	}
	
	public static String formatFileDate(File file) {
		Log.i("formatFileDate()", "BEGIN");
		String date = "";
		try {
			String fileDate = file.m_sName;
			if (fileDate.endsWith(file.m_sExtension)) {
				fileDate = fileDate.substring(0, fileDate.lastIndexOf(file.m_sExtension));
			}			
			SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
			Date tmpDate = formatter.parse(fileDate);			
			formatter = new SimpleDateFormat("dd.MM.yyyy - HH:mm:ss");
			date = formatter.format(new Date());
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("formatFileDate()", e.getMessage());
			}
		}		
		Log.i("formatFileDate()", "END");
		return date;
	}
	
	public static String getFileDate() {
		Log.i("getFileDate()", "BEGIN");
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
		String date = formatter.format(new Date());
		Log.i("getFileDate()", "END");
		return date;
	}
	
	public static long calculateDifference(long startTime, long endTime) {
		Log.i("calculateDifference()", "BEGIN");
		long difference = endTime - startTime;
		Log.i("calculateDifference()", "DIFFERENCE " + difference);
		Log.i("calculateDifference()", "END");
		return difference;
	}
	
	public static String getDuration(long startTime, long endTime) {
		Log.i("getDuration()", "BEGIN");
		long duration = calculateDifference(startTime, endTime);
		
		long seconds = (duration / 1000) % 60;
		long minutes = (duration / (1000 * 60)) % 60;
		long hours = (duration / (1000 * 60 * 60)) % 24;
		
		String formattedDuration = String.format("%02d:%02d:%02d", hours, minutes, seconds);
		Log.i("calculateDifference()", "DURATION " + formattedDuration);
		
		Log.i("calculateDifference()", "END");
		return formattedDuration;
	}
	
}
